import configparser
import importlib

class AnalyzerFactory:
    def __init__(self, cursor, config_path, thread_name):
        self.cursor = cursor
        self.thread_name = thread_name
        self.config = configparser.ConfigParser()
        self.config.optionxform = str
        self.config.read(config_path)
        self.register_analyzers()

    def register_analyzers(self):
        self.analyzers = {}
        keys = self.config.options("Analyzers")
        for key in keys:
            module_path = self.config.get("Analyzers", key)
            # 修改模块导入路径
            module_path = f"cold_start_analyzer.{module_path}"
            module_name, class_name = module_path.rsplit(".", 1)
            module = importlib.import_module(module_name)
            self.analyzers[key] = getattr(module, class_name)
        for key, analyzer in self.analyzers.items():
            if self.config.has_option(key, 'Children'):
                children_keys = self.config.get(key, 'Children').split(",")
                analyzer.children = [self.analyzers[child_key] for child_key in children_keys]

    def analyze(self):
        result = []
        for key, analyzer in self.analyzers.items():
            if key in ['Entry']:
                res = analyzer(self.cursor, thread_name=self.thread_name).analyze()
                res['name'] = key
                result.append(res)
        return result
        